<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use Mine\MineModel;

/**
 * @property int $id 主键
 * @property int $main_uid 主账号id
 * @property int $uid 私域用户ID
 * @property int $robot_id 机器人id
 * @property int $sop_id sop_id
 * @property int $template_id 模板id
 * @property int $to_id 发送对象id
 * @property int $is_room 1-群 0-个人
 * @property int $status 1-进行中  2-取消 3-暂停 4-完成 5-退订 6-流失
 * @property int $template_type 1标准模板  2标签sop模板 3新客户模板  4.群模板
 * @property int $day 总共多少天
 * @property int $complate_day 完成第几天
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $next_at 下次发送时间
 * @property string $object_name 
 * @property int $tirgger_type 触发类型
 * @property int $sub_task_id 子任务id
 * @property int $is_send_return 是否已发送return
 * @property int $is_return 是否支持退订
 * @property string $return_text 退订文字
 * @property string $return_code 退订口令
 * @property int $reply_total 有效回复次数
 * @property int $is_track 是否跟踪
 * @property int $track_limit 跟踪时间
 * @property-read null|\Hyperf\Database\Model\Collection|ChSopSendTask[] $task 
 * @property-read null|ChRoom $room 
 * @property-read null|ChContact $contact 
 * @property-read null|ChSopTemplate $template 
 */
class ChSopSendTaskMain extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_sop_send_task_main';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'main_uid', 'uid', 'robot_id', 'sop_id', 'template_id', 'to_id', 'is_room', 'status', 'template_type', 'day', 'complate_day', 'created_at', 'updated_at', 'next_at', 'object_name', 'tirgger_type', 'sub_task_id', 'is_send_return', 'is_return', 'return_text', 'return_code', 'reply_total', 'is_track', 'track_limit'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'main_uid' => 'integer', 'uid' => 'integer', 'robot_id' => 'integer', 'sop_id' => 'integer', 'template_id' => 'integer', 'to_id' => 'integer', 'is_room' => 'integer', 'status' => 'integer', 'template_type' => 'integer', 'day' => 'integer', 'complate_day' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'tirgger_type' => 'integer', 'sub_task_id' => 'integer', 'is_send_return' => 'integer', 'is_return' => 'integer', 'reply_total' => 'integer', 'is_track' => 'integer', 'track_limit' => 'integer'];

    public function task()
    {
        return $this -> hasMany(ChSopSendTask::class,'task_main_id','id');
    }

    public function room()
    {
        return $this -> hasOne(ChRoom::class,'room_wxid','to_id');
    }

    public function contact()
    {
        return $this -> hasOne(ChContact::class,'user_id','to_id');
    }

    public function template()
    {
        return $this -> hasOne(ChSopTemplate::class,'id','template_id');
    }

    public static function saveMain($sop,$robot_id,$to_id,$day,$object_name,$tirgger_type,$is_return,$return_text,$return_code,$track_limit,$is_track=0,$is_room=0,$subTaskId=0)
    {
        $model = new self();
        $model->main_uid=$sop['main_uid'];
        $model->uid=$sop['uid'];
        $model->robot_id=$robot_id;
        $model->sop_id=$sop['id'];
        $model->template_id=$sop['template_id'];
        $model->template_type=$sop['template_type'];
        $model->to_id=$to_id;
        $model->day=$day;
        $model->is_room=$is_room;
        $model->object_name=$object_name;
        $model->tirgger_type=$tirgger_type;
        $model->sub_task_id=$subTaskId;
        $model->is_return=$is_return;
        $model->return_text=$return_text;
        $model->return_code=$return_code;
        $model->is_track=$is_track;
        $model->track_limit=$track_limit;
        $model->save();
        return $model;
    }
}
